package com.huawei.hgip.paintgame.util;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import android.graphics.Bitmap;
import android.os.Message;

public class VoteT extends Thread {

	private byte voteType = 0;
	private String fileName = null;
	private Socket socket = null;
	private DataOutputStream os = null;

	/**
	 * 投票
	 * @param vote 投票结果 1 good 2 bad
	 * @param vFileName 投票对象文件名 
	 */
	public VoteT(byte vote, String vFileName) {
		this.voteType = vote;
		this.fileName = vFileName;
		this.start();
	}

	public void run() {

		try {
			socket = PGUtil.getSocket(PGUtil.BC_PIC_SEARCH_VOTE);
			os = new DataOutputStream(socket.getOutputStream());
			sendVote(os);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				os.flush();
				os.close();
				socket.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * 发送投票。文件大小不能大于 {@link Integer#MAX_VALUE}
	 * 
	 * @param hostname
	 *            接收端主机名或 IP 地址
	 * @param port
	 *            接收端端口号
	 * @param filepath
	 *            文件路径
	 * 
	 * @throws IOException
	 *             如果读取文件或发送失败
	 */
	public void sendVote(OutputStream os) throws IOException {
		os.write(this.voteType);// 写投票类型
		writeVoteInfo(fileName, os);	
	}
	
	// 输出投票人和pic name
	private void writeVoteInfo(String fileName, OutputStream os) throws IOException {
		byte[] fn_bytes = fileName.getBytes();
		os.write(PGUtil.i2b(fn_bytes.length)); // 输出VoteInfo长度
		os.write(fn_bytes); // 输出VoteInfo
	}
}